package com.google.gsaguide.volley;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    /**
     * This method can be used to get the AppController instance.
     * @return - AppController instance.
     */
    public static synchronized AppController getInstance() {
        return mInstance;
    }

    /**
     * This method can be used to get the instance of RequestQueue.
     * @return - Instance of RequestQueue.
     */
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    /**
     * This method can be used to get the instance of ImageLoader.
     * @param context - Current context.
     * @return - Instance of ImageLoader.
     */
    public ImageLoader getImageLoader(Context context) {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache(context));
        }
        return this.mImageLoader;
    }

    /**
     * This method allows to add a request with a specific tag.
     * @param req - Request to add.
     * @param tag - Tag of this request.
     * @param <T> - Type of this request.
     */
    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // Set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    /**
     * This method allows to add a request to RequestQueue.
     * @param req - Request to add.
     * @param <T> - Type of this request.
     */
    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    /**
     * This method allows to cancel all the pending requests.
     */
    public void cancelAllPendingRequests() {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(new RequestQueue.RequestFilter() {
                @Override
                public boolean apply(Request<?> request) {
                    return true;
                }
            });
        }
    }

    /**
     * This method allows to cancel a request using a tag.
     * @param tag - Tag of the request.
     */
    public void cancelPendingRequest(String tag){
        mRequestQueue.cancelAll(tag);
    }

    /**
     * This method allows to clean the image cache.
     * @param context - The current context.
     */
    public void clearImageCache(Context context){
        LruBitmapCache.clear(context);
    }
}
